package com.example.tianwu.daydayup;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewFlipper;

import java.util.ArrayList;

public class HistoryActivity extends Activity {

    private ViewFlipper viewFlipper;

    private ArrayList<Photo> photos;

    private TextView dateText;
    private TextView beautyValue;
    private TextView smallValue;

    int nowPhoto = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);

        viewFlipper = (ViewFlipper)findViewById(R.id.viewFlipper);
        dateText = (TextView)findViewById(R.id.dateText);
        beautyValue = (TextView)findViewById(R.id.beautyValue);
        smallValue = (TextView)findViewById(R.id.smallValue);

        photos = SaveData.readPhotos(this);

        //加载UI测试图片
        ImageView imageView1 = new ImageView(this);
        imageView1.setImageResource(R.drawable.test_1);
        ImageView imageView2 = new ImageView(this);
        imageView2.setImageResource(R.drawable.test_2);
        ImageView imageView3 = new ImageView(this);
        imageView3.setImageResource(R.drawable.test_3);
        viewFlipper.addView(imageView1);
        viewFlipper.addView(imageView2);
        viewFlipper.addView(imageView3);

        //加载所有历史图片
        for(int i=0;i<photos.size();i++) {
            ImageView imageView = new ImageView(this);
            Bitmap bp = BitmapFactory.decodeFile(photos.get(i).getFileName());
            imageView.setImageBitmap(bp);
            viewFlipper.addView(imageView);
        }
        if(!photos.isEmpty()) {
            Photo p = photos.get(nowPhoto);
            dateText.setText(p.getDate());
            beautyValue.setText(Integer.toString(p.getFaceData().beauty));
            smallValue.setText(Integer.toString(p.getFaceData().expression));
        }
    }
    public void toNext(View view){
        viewFlipper.showNext();
        if(!photos.isEmpty()) {
            nowPhoto = ++nowPhoto % photos.size();
            Photo p = photos.get(nowPhoto);
            dateText.setText(p.getDate());
            beautyValue.setText(Integer.toString(p.getFaceData().beauty));
            smallValue.setText(Integer.toString(p.getFaceData().expression));
        }
    }
    public void toPrev(View view){
        viewFlipper.showPrevious();
        if(!photos.isEmpty()) {
            nowPhoto = ++nowPhoto % photos.size();
            Photo p = photos.get(nowPhoto);
            dateText.setText(p.getDate());
            beautyValue.setText(Integer.toString(p.getFaceData().beauty));
            smallValue.setText(Integer.toString(p.getFaceData().expression));
        }
    }
    public void clickReturn(View view){
        this.finish();
    }

}
